let utils = require('../../utils/util')
//初始化云数据库环境
let db = wx.cloud.database({
    env: 'linyr-6gp7qo8008360a60'
});
//链接云数据库
let pet = db.collection('db_pet')
let look = db.collection('db_look')

Page({

    /**
     * 页面的初始数据
     */
    data: {
        petList: [],
        knowInfo:{}
    },

    //获取随机列表
    async listPet() {
        let that = this;
        let count = 0;
        await pet.count().then(content => {
            // console.log(content);
            let {
                total,
                errMsg
            } = content
            if (errMsg == "collection.count:ok") {
                count = total;
            }
        })
        console.log("长度==>", count);
        wx.showLoading({
            title: '加载中...',
            icon: "none"
        })
        let arr = []
        let numlist = []
        for (let i = 0; numlist.length < 4; i++) {
            let num = Math.floor(Math.random() * count)
            if (!numlist.includes(num)) {
                numlist.push(num)
            }
        }
        // console.log("1111===>",numlist);

        for (let j = 0; j < numlist.length; j++) {
            // console.log("随机数==>",num);
            await pet.skip(numlist[j]).limit(1).get().then(content => {
                // console.log(content);
                let {
                    data,
                    errMsg
                } = content
                if (errMsg == 'collection.get:ok') {
                    arr = arr.concat(data)
                    // console.log("数组==>", this.data.petList);
                }
            })

        }
        that.setData({
            petList: arr
        })

        wx.hideLoading()

    },

    //去列表详情
    //  goClassifyList(event){
    //     // console.log(event);
    //     if(event.currentTarget.dataset.type){
    //         let type = event.currentTarget.dataset.type
    //         console.log(type);
    //         pet.where({pet_type:Number(type)}).get().then(content=>{
    //             console.log(content);
    //         })
    //     }
    // },
    //去详情页
    async goDetail(event){
        // console.log(event);
        let _id = event.currentTarget.dataset.id
        let dt = new Date()
        let look_time = utils.formatTime(dt)
        await look.where({look_id:_id}).get().then(content=>{
            // console.log(content);
            let { data,errMsg } = content
            if(errMsg=='collection.get:ok'){
                if(data.length==0){
                    pet.where({_id}).get().then(content=>{
                        console.log('2.0',content);
                        let { data,errMsg } = content
                        if(errMsg=='collection.get:ok'){
                            let look_img = data[0].pet_coverURL
                            let look_name = data[0].pet_name
                            look.add({
                                data:{
                                    look_id:_id,
                                    look_time,
                                    look_img,
                                    look_name
                                }
                            })
                        }
                    })
                }
            }
        })
        wx.navigateTo({
          url: '../detail/detail?id='+_id,
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        // console.log(options.id);
        this.listPet();
        let id = options.id
        if (id) {

            pet.where({
                _id: id
            }).get().then(content => {
                console.log(content);
                let { data , errMsg } = content
                if(errMsg == 'collection.get:ok'){
                    this.setData({
                        knowInfo:data[0]
                    })
                }
            })
        }

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})